#include "WebMonitorDialog.h"
#include <regex>

WebMonitorDialog::WebMonitorDialog(wxWindow* parent) : wxDialog(parent,wxID_ANY,wxT("远程设置"))
{
    wxFlexGridSizer* mainSizer=new wxFlexGridSizer(2,5,10);
    wxBoxSizer* btnSizer=new wxBoxSizer(wxHORIZONTAL);
    txtIPAddress=new wxTextCtrl(this,wxID_ANY);
    txtPort=new wxTextCtrl(this,wxID_ANY);
    txtDeviceName=new wxTextCtrl(this,wxID_ANY);
    btnCancel=new wxButton(this,(int)WebMonitorDialogID::btnCancel,wxT("返回"));
    btnSaveSetting=new wxButton(this,(int)WebMonitorDialogID::btnSaveSetting,wxT("保存"));
    mainSizer->Add(new wxStaticText(this,wxID_ANY,wxT("IP地址")),wxSizerFlags(1));
    mainSizer->Add(txtIPAddress,wxSizerFlags(2).Expand());
    mainSizer->Add(new wxStaticText(this,wxID_ANY,wxT("端口号")),wxSizerFlags(1));
    mainSizer->Add(txtPort,wxSizerFlags(2).Expand());
    mainSizer->Add(new wxStaticText(this,wxID_ANY,wxT("设备名称")),wxSizerFlags(1));
    mainSizer->Add(txtDeviceName,wxSizerFlags(2).Expand());
    mainSizer->AddStretchSpacer();
    btnSizer->Add(btnSaveSetting);
    btnSizer->Add(btnCancel);
    mainSizer->Add(btnSizer);
    SetSizerAndFit(mainSizer);

    Bind(wxEVT_BUTTON,&WebMonitorDialog::on_btnCancel_click,this,(int)WebMonitorDialogID::btnCancel);
    Bind(wxEVT_BUTTON,&WebMonitorDialog::on_btnSaveSetting_click,this,(int)WebMonitorDialogID::btnSaveSetting);
}

std::string WebMonitorDialog::GetIPAddress()
{
    return txtIPAddress->GetValue().ToStdString();
}

std::string WebMonitorDialog::GetPort()
{
    return txtPort->GetValue().ToStdString();
}

std::string WebMonitorDialog::GetDeviceName()
{
    return txtDeviceName->GetValue().ToStdString();
}

void WebMonitorDialog::on_btnCancel_click(wxCommandEvent& event)
{
    EndModal(0);
}

void WebMonitorDialog::on_btnSaveSetting_click(wxCommandEvent& event)
{
    std::ofstream configFile("./config",std::ios_base::trunc);
    if(configFile.is_open())
    {
        configFile<<txtIPAddress->GetValue().ToStdString()<<std::endl;
        configFile<<txtPort->GetValue().ToStdString()<<std::endl;
        configFile<<txtDeviceName->GetValue().ToStdString()<<std::endl;
        configFile.close();
    }
    EndModal(1);
}